package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

@Repository
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public boolean outMoney(Long accountId, BigDecimal amount) {
        String sql = "UPDATE account SET balance = balance - ? WHERE id = ? AND balance >= ?";
        int rows = jdbcTemplate.update(sql, amount, accountId, amount);
        return rows > 0;
    }

    @Override
    public boolean inMoney(Long accountId, BigDecimal amount) {
        String sql = "UPDATE account SET balance = balance + ? WHERE id = ?";
        int rows = jdbcTemplate.update(sql, amount, accountId);
        return rows > 0;
    }
}